package com.ph.shopping.facade.member.service.dbservice.facade.impl;

import java.util.Date;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ph.shopping.common.core.constant.SmsCodeConstant;
import com.ph.shopping.common.core.customenum.MemberEnum;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.customenum.SmsCodeType;
import com.ph.shopping.common.core.other.smssend.SmsCodeService;
import com.ph.shopping.common.core.other.smssend.util.SmsSendUtil;
import com.ph.shopping.common.util.container.ContainerUtil;
import com.ph.shopping.common.util.date.ThreadLocalDateUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.facade.duboresult.DubboResult;
import com.ph.shopping.facade.mapper.SmsCodeMapper;
import com.ph.shopping.facade.member.entity.SmsCodeSendinfo;
import com.ph.shopping.facade.member.service.dbservice.facade.ISmsSendCodeDbService;

@Service
public class SmsSendCodeDbService implements ISmsSendCodeDbService{

	private static final Logger logger = LoggerFactory.getLogger(SmsSendCodeDbService.class);
	
	@Autowired
	private SmsCodeMapper smsCodeMapper;

	@Autowired
	private SmsCodeService smsCodeService;

	@Override
	public Result querySmsCodeInfoByMobile(String mobile) {
		Result result = DubboResult.getDefResultByImpl();
		SmsCodeSendinfo smsCodeInfo = smsCodeMapper.selectSmsCodeInfoByMonile(mobile);
		if (smsCodeInfo != null) {
			result.setData(smsCodeInfo);
			result = DubboResult.getResultBySuccess(result);
		}
		return result;
	}

	@Override
	public Result verifySmsCode(String phone, String type, String code) {
		Result result = DubboResult.getDefResultByImpl();
		Map<String, Object> map = ContainerUtil.map();
		map.put("mobile", phone);
		map.put("smsCode", code);
		map.put("sendType", type);
		SmsCodeSendinfo smsCodeInfo = smsCodeMapper.selectSmsCodeInfo(map);
		if (smsCodeInfo != null && StringUtils.isNotBlank(smsCodeInfo.getSmsCode())) {
			Date createTime = smsCodeInfo.getCreateTime();
			logger.debug("createTime : " + createTime);
			if (createTime != null) {
				if (MemberEnum.SMS_AVAILABLE.getCode().equals(smsCodeInfo.getIsAvailable())
						&& ThreadLocalDateUtil.dateDifferSeconds(createTime) < SmsCodeConstant.SMS_EFFECTIVE_TIME) {
					updateSmsCodeInfo(smsCodeInfo);
					result = DubboResult.getResultBySuccess(result);
				} else {
					result = DubboResult.getResultByEnum(ResultEnum.SMS_CODE_INVALID);
				}
			}
		} else {
			result = DubboResult.getResultByEnum(ResultEnum.SMS_CODE_ERROR);
		}
		return result;
	}

	/**
	 * 
	 * @Title: updateSmsCodeInfo   
	 * @Description: 修改短信验证码状态   
	 * @param: @param info
	 * @param: @return      
	 * @return: boolean      
	 * @throws
	 */
	@Transactional
	private boolean updateSmsCodeInfo(SmsCodeSendinfo info){
		SmsCodeSendinfo updateInfo = new SmsCodeSendinfo();
		updateInfo.setUpdateTime(new Date());
		updateInfo.setIsAvailable(MemberEnum.SMS_INVALID.getCode());
		updateInfo.setPhoneNumber(info.getPhoneNumber());
		smsCodeMapper.updateSmsCodeInfo(updateInfo);
		return true;
	}
	@Override
	@Transactional
	public Result sendSmsCodeByNoMsg(String phone, String type) {

		return senSmsCodeByResult(phone, type, SmsSendUtil.getSmsCode());
	}

	@Override
	@Transactional
	public Result sendSmsCodeByCustomMsg(String phone, String type, String message) {

		return senSmsCodeByResult(phone, type, message);
	}
	/**
	 * 
	 * @Title: senSmsCodeByResult   
	 * @Description: 发送短信及信息入库信息   
	 * @param: @param phone
	 * @param: @param type
	 * @param: @param message
	 * @param: @return      
	 * @return: Result      
	 * @throws
	 */
	private Result senSmsCodeByResult(String phone, String type, String message){
		Result result = DubboResult.getResultByEnum(ResultEnum.MEMBER_FAILURE);
		SmsCodeSendinfo info = new SmsCodeSendinfo();
		info.setCreateTime(new Date());
		info.setPhoneNumber(phone);
		info.setSendType(type);
		if (sendMsg(phone, type, message)) {
			info.setSendStatus(MemberEnum.SMS_SEND.getCode());
			result = DubboResult.getResultBySuccess(result);
		} else {
			info.setSendStatus(MemberEnum.SMS_SEND_FAIL.getCode());
		}
		//if (SmsCodeType.SEND_USER_PWD.getCodeType().equals(type)) {
			// 当发送的是密码时则加密短信内容
			//message = MD5.getMD5Str(message);
		//}
		info.setSmsCode(message);
		info.setIsAvailable(MemberEnum.SMS_AVAILABLE.getCode());
		smsCodeMapper.insert(info);
		return result;
	}
	/**
	 * 
	 * @Title: sendMsg   
	 * @Description: 执行发送操作   
	 * @param: @param phone
	 * @param: @param type
	 * @param: @param message
	 * @param: @return      
	 * @return: boolean      
	 * @throws
	 */
	private boolean sendMsg(String phone, String type, String message) {
		Result result = null;
		if (SmsCodeType.SEND_USER_PWD.getCodeType().equals(type)) {
			result = smsCodeService.sendSmsCodeByPassword(phone, message);
		} else {
			result = smsCodeService.sendSmsCodeBySmsCode(phone, message);
		}
		return result != null && ResultEnum.SUCCESS.getCode().equals(result.getCode());
	}

}
